package com.secondGroup.serviceImpl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.secondGroup.config.OperationNum;
import com.secondGroup.entity.User;
import com.secondGroup.entity.UserOperate;
import com.secondGroup.mapper.UserMapper;
import com.secondGroup.service.UserOperateService;
import com.secondGroup.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.secondGroup.utils.IpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ws
 * @since 2022-05-05
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    private Map<String,String> cerificationCodeMap  = new ConcurrentHashMap<String,String>();

    @Autowired
    private UserOperateService userOperateService;

    @Autowired
    private UserMapper userMapper;

    /**
     * 用户登录
     * @param loginuser
     * @return 返回 0 表示登录失败；返回 1 表示登录成功
     */
    @Override
    public HashMap userLogin(User loginuser,HttpServletRequest response) {
        HashMap<Integer, Object> userLoginHashMap = new HashMap<>();
        if (loginuser.getAccount() == null) {
            userLoginHashMap.put(0, "用户名或密码不正确");
            return userLoginHashMap;
        }

        if (loginuser.getLoginPassword() == null) {
            userLoginHashMap.put(0, "用户名或密码不正确");
            return userLoginHashMap;
        }
        HashMap<String, Object> userLoginMap = new HashMap<>();
        userLoginMap.put("account", loginuser.getAccount());
        userLoginMap.put("login_password", loginuser.getLoginPassword());
        List<User> users = listByMap(userLoginMap);
        if (users.size() == 0 | users == null ) {
            userLoginHashMap.put(0, "用户名或密码不正确");
            return userLoginHashMap;
        } else {
            // 修改用户表用户登录状态
            User user = new User();
            user.setLoginStatus(1);
            user.setAccount(loginuser.getAccount());
            userMapper.updateLoginStatus(user);
            // 在记录用户的操作记录用户操作
            UserOperate userOperate = new UserOperate();
            userOperate.setOperateIp(IpUtil.getIpAddr(response));
            userOperate.setOperateTime(LocalDateTime.now());
            userOperate.setOpereateType(OperationNum.LOGIN);
            userOperateService.save(userOperate);
            List<User> users1 = userMapper.selectByUser(user.getAccount());
            userLoginHashMap.put(1, users1);
            return userLoginHashMap;
        }
    }
    /**
     * 用户注册
     * @param logonUser
     * @return 返回 0 表示注册失败；返回 1 表示注册成功
     */
    @Override
    public HashMap<Integer,Object> userLogon(User logonUser,HttpServletRequest response) {
        HashMap<Integer, Object> userLogonHashMap = new HashMap<>();
        if (logonUser.getAccount() == null){
            userLogonHashMap.put(0, "请输入用户名");
            return userLogonHashMap;
        }
        if (logonUser.getName() == null) {
            userLogonHashMap.put(0, "请输入姓名");
            return userLogonHashMap;
        }
        if (logonUser.getLoginPassword() == null) {
            userLogonHashMap.put(0, "请输入密码");
            return userLogonHashMap;
        }
        if (logonUser.getPhone() == null) {
            userLogonHashMap.put(0, "请输入电话");
            return userLogonHashMap;
        }
        if (logonUser.getEmail() == null) {
            userLogonHashMap.put(0, "请输入邮箱");
            return userLogonHashMap;
        }
        logonUser.setRegisterTime(LocalDateTime.now()); //获取系统当前时间，作为用户的注册时间
        List<User> byAccount = userMapper.selectByUser(logonUser.getAccount());
        if (byAccount.size()==0|byAccount == null){
            save(logonUser);
            // 记录用户操作
            UserOperate userOperate = new UserOperate();
            userOperate.setOperateIp(IpUtil.getIpAddr(response));
            userOperate.setOperateTime(LocalDateTime.now());
            userOperate.setOpereateType(OperationNum.LOGON);
            userOperateService.save(userOperate);
            userLogonHashMap.put(1, "注册成功");
            return userLogonHashMap;
        }else {
            userLogonHashMap.put(0, "此账户以注册请登录");
            return userLogonHashMap;
        }
    }

    /**
     * 获取验证码
     * @return
     */
    @Override
    public HashMap<Integer,Object> getCerificationCode(String phone) {
        HashMap<Integer, Object> userLoginHashMap = new HashMap<>();
        // 判断该手机号是否以及注册过账号
        List<User> users = userMapper.selectByPhone(phone);
        if (users.size()==0|users == null){
            userLoginHashMap.put(0,"该手机号未注册，请注册后登录");
            return userLoginHashMap;
        }
        String[] beforeShuffle = new String[] { "2", "3", "4", "5", "6", "7",
                "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J",
                "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
                "W", "X", "Y", "Z" };
        List list = Arrays.asList(beforeShuffle);
        Collections.shuffle(list);
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            sb.append(list.get(i));
        }
        String afterShuffle = sb.toString();
        String result = afterShuffle.substring(5, 9);
        cerificationCodeMap.put(phone,result);
        userLoginHashMap.put(1,"验证码为："+result);
        userLoginHashMap.put(2,phone);
        return userLoginHashMap;
    }

    /**
     * 手机号登录
     * @param phone
     * @param code
     * @return
     */
    @Override
    public HashMap<Integer, Object> phoneLogin(String phone, String code) {
        HashMap<Integer, Object> userLoginHashMap = new HashMap<>();
        if (phone == null){
            userLoginHashMap.put(0, "请输入手机号");
            return userLoginHashMap;
        }
        if (code == null){
            userLoginHashMap.put(0, "请输入验证码");
            return userLoginHashMap;
        }
        if (cerificationCodeMap.get(phone)==null){
            userLoginHashMap.put(0, "请重新获取验证码");
            return userLoginHashMap;
        }
        if (code.equals(cerificationCodeMap.get(phone))){
            cerificationCodeMap.remove(phone);
            List<User> users = userMapper.selectByPhone(phone);
            userLoginHashMap.put(1, users);
            return userLoginHashMap;
        }else {
            userLoginHashMap.put(0, "请输入正确验证码");
            userLoginHashMap.remove(phone);
            return userLoginHashMap;
        }
    }

}
